﻿namespace iToolPlugin.SMSProvider
{
    public class SmsSingleSender : SmsBase
    {
        private string url = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms";

        public SmsSingleSender(int appid, string appkey)
            : base(appid, appkey, new DefaultHTTPClient())
        {
        }

        public SmsSingleSender(int appid, string appkey, IHTTPClient httpclient)
            : base(appid, appkey, httpclient)
        {
        }

        public SmsSingleSenderResult send(int type, string nationCode, string phoneNumber, string msg, string extend, string ext)
        {
            long random = SmsSenderUtil.getRandom();
            long currentTime = SmsSenderUtil.getCurrentTime();
            JSONObjectBuilder jSONObjectBuilder = new JSONObjectBuilder().Put("tel", new JSONObjectBuilder().Put("nationcode", nationCode).Put("mobile", phoneNumber).Build()).Put("type", type).Put("msg", msg)
                .Put("sig", SmsSenderUtil.calculateSignature(base.appkey, random, currentTime, phoneNumber))
                .Put("time", currentTime)
                .Put("extend", (!string.IsNullOrEmpty(extend)) ? extend : "")
                .Put("ext", (!string.IsNullOrEmpty(ext)) ? ext : "");
            HTTPRequest request = new HTTPRequest(HTTPMethod.POST, url).addHeader("Conetent-Type", "application/json").addQueryParameter("sdkappid", base.appid).addQueryParameter("random", random)
                .setConnectionTimeout(60000)
                .setRequestTimeout(60000)
                .setBody(jSONObjectBuilder.Build().ToString());
            HTTPResponse response = base.httpclient.fetch(request);
            handleError(response);
            SmsSingleSenderResult smsSingleSenderResult = new SmsSingleSenderResult();
            smsSingleSenderResult.parseFromHTTPResponse(response);
            return smsSingleSenderResult;
        }

        public SmsSingleSenderResult sendWithParam(string nationCode, string phoneNumber, int templateId, string[] parameters, string sign, string extend, string ext)
        {
            long random = SmsSenderUtil.getRandom();
            long currentTime = SmsSenderUtil.getCurrentTime();
            JSONObjectBuilder jSONObjectBuilder = new JSONObjectBuilder().Put("tel", new JSONObjectBuilder().Put("nationcode", nationCode).Put("mobile", phoneNumber).Build()).Put("sig", SmsSenderUtil.calculateSignature(base.appkey, random, currentTime, phoneNumber)).Put("tpl_id", templateId)
                .PutArray("params", parameters)
                .Put("sign", (!string.IsNullOrEmpty(sign)) ? sign : "")
                .Put("time", currentTime)
                .Put("extend", (!string.IsNullOrEmpty(extend)) ? extend : "")
                .Put("ext", (!string.IsNullOrEmpty(ext)) ? ext : "");
            HTTPRequest request = new HTTPRequest(HTTPMethod.POST, url).addHeader("Conetent-Type", "application/json").addQueryParameter("sdkappid", base.appid).addQueryParameter("random", random)
                .setConnectionTimeout(60000)
                .setRequestTimeout(60000)
                .setBody(jSONObjectBuilder.Build().ToString());
            HTTPResponse response = base.httpclient.fetch(request);
            handleError(response);
            SmsSingleSenderResult smsSingleSenderResult = new SmsSingleSenderResult();
            smsSingleSenderResult.parseFromHTTPResponse(response);
            return smsSingleSenderResult;
        }
    }
}
